package la.iok.hzsvn.lewin.movie.config;

import la.iok.hzsvn.lewin.movie.obs.HuaweiObsServiceImpl;
import la.iok.hzsvn.lewin.movie.obs.LocalFileObsService;
import la.iok.hzsvn.lewin.movie.obs.ObsService;
import la.iok.hzsvn.lewin.movie.obs.TencentObsServiceImpl;
import la.iok.hzsvn.lewin.movie.service.HuaweiObsFileSystemServiceImpl;
import la.iok.hzsvn.lewin.movie.service.LocalFileSystemServiceImpl;
import la.iok.hzsvn.lewin.movie.service.TencentCosFileSystemServiceImpl;
import la.iok.hzsvn.redis.service.RedisService;
import la.iok.hzsvn.share.dfs.service.DfsService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
@EnableConfigurationProperties(DfsProperties.class)
public class DfsConfiguration {
    @Configuration
    @ConditionalOnProperty(name = "app.dfs.type",havingValue = "local")
    @EnableConfigurationProperties(LocalFileSystemProperty.class)
    public static class LocalFileDfsConfiguration {
        @Bean
        public ObsService obsService() {
            return new LocalFileObsService();
        }
        @Bean("dfsService")
        public DfsService localDfsService(LocalFileSystemProperty localFileSystemProperty){
            return new LocalFileSystemServiceImpl(localFileSystemProperty);
        }
    }

    @Configuration
    @ConditionalOnProperty(name = "app.dfs.type",havingValue = "huawei")
    @EnableConfigurationProperties(HuaweiServerProperties.class)
    public static class HuaweiObsDfsConfiguration {
        @Bean
        public ObsService obsService(HuaweiServerProperties huaweiServerProperties, RestTemplate restTemplate,
                                     RedisService redisService, RedisKey redisKey) {
            return new HuaweiObsServiceImpl(huaweiServerProperties, restTemplate, redisService, redisKey);
        }
        @Bean("dfsService")
        public DfsService huaweiDfsService(ObsService obsService){
            return new HuaweiObsFileSystemServiceImpl(obsService);
        }
    }

    @Configuration
    @ConditionalOnProperty(name = "app.dfs.type",havingValue = "tencent")
    @EnableConfigurationProperties(TencentCosServerProperties.class)
    public static class TencentCosDfsConfiguration {
        @Bean
        public ObsService obsService(TencentCosServerProperties tencentCosServerProperties,
                                     RedisService redisService, RedisKey redisKey) {
            return new TencentObsServiceImpl(tencentCosServerProperties, redisService, redisKey);
        }
        @Bean("dfsService")
        public DfsService tencentCosDfsService(ObsService obsService){
            return new TencentCosFileSystemServiceImpl(obsService);
        }
    }
}
